package user_private_key

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/services/user_private_key"
	"net/http"
)

func (h *handler) Update() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		//userInfo := ctx.GetInfo("userInfo").(*user.User)
		createData := new(user_private_key.UserPrivateKeyData)
		createData.Id = req.Id
		createData.CollectAddress = req.CollectAddress
		createData.FeeAddress = req.FeeAddress
		//createData.WithdrawAddress = req.WithdrawAddress
		err := h.keyService.Update(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.UserPrivateKeyUpdateError,
				code.Text(code.UserPrivateKeyUpdateError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
